﻿using Microsoft.Data.SqlClient;
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UsedBook.Models;
using UsedBook.Utils;

namespace UsedBook.DAL
{
    public class CollegeDAL
    {
        // 查询所有学院
        public List<College> GetAllCollege()
        {
            using (SqlConnection conn  = DBHelper.GetSqlConnection())
            {
                conn.Open();
                List<College> colleges = new List<College>();
                string sql = "select * from College";
                using (SqlCommand cmd = new(sql, conn))
                {
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        College college = new College();
                        college.CollegeId = reader.GetInt32(reader.GetOrdinal("CollegeId"));
                        college.CollegeName = reader.GetString(reader.GetOrdinal("CollegeName"));
                        colleges.Add(college);
                    }
                    return colleges;
                }
            }
        }

        // 删除某个学院
        public bool DeleteCollege(int id)
        {
            using(SqlConnection conn = DBHelper.GetSqlConnection())
            {
                conn.Open();
                string sql = "delete from College where CollegeId = @Id";
                SqlParameter sqlParameter = new SqlParameter("Id",id);
                using (SqlCommand cmd = new(sql,conn))
                {
                    cmd.Parameters.Add(sqlParameter);
                    int rows = cmd.ExecuteNonQuery();
                    return rows > 0;
                }
            }
        }


    }
}
